﻿<html>
<head>
    <script>
	var point=0;
	var elements=new Array();
	function toggleFullScreen() {
    if (!document.fullscreenElement && // alternative standard method
        !document.mozFullScreenElement && !document.webkitFullscreenElement) {// current working methods
        if (document.documentElement.requestFullscreen) {
            document.documentElement.requestFullscreen();
        } else if (document.documentElement.mozRequestFullScreen) {
            document.documentElement.mozRequestFullScreen();
        } else if (document.documentElement.webkitRequestFullscreen) {
            document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
        }
    } else {
        if (document.cancelFullScreen) {
            document.cancelFullScreen();
        } else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
        } else if (document.webkitCancelFullScreen) {
            document.webkitCancelFullScreen();
        }
    }
}
	function getleft(obj)
	{
		return parseInt(obj.style.left.substr(0, obj.style.left.length - 2))
	}
	function addtop(obj,add)
	{
		return parseInt(obj.style.top.substr(0, obj.style.top.length - 2));
	}
	function addleft(obj,add)
	{
		var lft=obj.style.left.substr(0, obj.style.left.length - 2);
		if(parseInt(parseInt(lft)+add)<=880&&parseInt(parseInt(lft)+add)>=0){
			obj.style.left=parseInt(parseInt(lft)+add)+"px";
		}
		
	}
	function addtop(obj,add)
	{
		var tp=obj.style.top.substr(0, obj.style.top.length - 2);
		if(parseInt(parseInt(tp)+add)<=880&&parseInt(parseInt(tp)+add)>=0){
			obj.style.top=parseInt(parseInt(tp)+add)+"px";
		}
	}
        window.onload=()=>{
         window.onkeydown=(e)=>{
			font.innerHTML="分数："+point;
                       if(e.keyCode==37)
                       {
							addleft(master,-80);
                       }
                       if(e.keyCode==39)
                       {
                            addleft(master,80);
                       }
                       if(e.keyCode==40)
                       {
                           
                            addtop(master,80);
                       }
                       if(e.keyCode==38)
                       {
                           
                           addtop(master,-80);
                       }
					   if(e.keyCode==38||e.keyCode==39||e.keyCode==40||e.keyCode==41)
					   {
							if(Math.random()>0.8)
							{
							elements.push(document.createElement('div'));
							background.appendChild(elements[elements.length-1]);
							elements[elements.length-1].innerHTML="student";
							elements[elements.length-1].style.height="80px";
							elements[elements.length-1].style.width="80px";
							elements[elements.length-1].style.top="0px";
							elements[elements.length-1].style.left="800px";
							elements[elements.length-1].style.position="absolute";
							elements[elements.length-1].style.innerHTML="student";
							elements[elements.length-1].style.backgroundColor="red";
							}
							for(var i=0;i<elements.length;i++)
							{
								if(elements[i].style.top==master.style.top&&elements[i].style.left==master.style.left)
								{
									//if(Math.random>0.5){
									//alert("您打死了一个学生！");
									
									point++;
									//}
								}
								/*if(elements[i].style.top-master.style.top<=80&&elements[i].style.left-master.style.left<=80)
								{
									//if(Math.random>0.5){
										alert("学生打了您一下");
										point--;
									}
								}*/
								if(Math.random()>0.5)
								{
									
									if(Math.random()>0.3)
									{
										addleft(elements[i],-80);
									}
									else
									{
										addleft(elements[i],80);
									}
								}
								else
								{
									if(Math.random()>0.3)
									{
										addtop(elements[i],-80);
									}
									else
									{
										addtop(elements[i],80);
									}
								}
							}
					   }
				
            }
            
        }
    </script>

</head>
<body>
<style>
    #master
    {

    }
</style>
<div id="score"><center><font id="font" size="20px" color="red">分数：</font></center></div>
<div id="background" style="background-color:blue;	position:absolute;        height:960px;        width:960px;">
	<div id="master" style="background-color:yellow;	position:relative;        height:80px;        width:80px;        left:0px;        top:0px;text-align: center;z-index:100">master</div>
</div>
	
<button onclick="window.onkeydown(37)" style="">↑</button>
<button onclick="window.onkeydown(38)">↓</button>
<button onclick="window.onkeydown(39)">←</button>
<button onclick="window.onkeydown(40)">→</button>
<!---<div id="global" style="width:100%;height:100%"></div>-->
</body>
</html>
